package com.yanqingshan.platform.module.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yanqingshan.platform.common.core.convert.BasePageListConvert;
import com.yanqingshan.platform.common.core.domain.BasePageList;
import com.yanqingshan.platform.common.core.domain.PageQuery;
import com.yanqingshan.platform.module.sys.mapper.SysRoleMapper;
import com.yanqingshan.platform.module.sys.model.domain.SysRole;
import com.yanqingshan.platform.module.sys.model.vo.SysRoleReqVO;
import com.yanqingshan.platform.module.sys.service.SysRoleService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能描述:系统角色  Service业务层处理
 *
 * @author yanqs
 * @since 2023-05-15
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Override
    public BasePageList<SysRole> pageCustom(SysRoleReqVO param, PageQuery query) {
        Page<SysRole> resultPage = new Page(query.getPage(), query.getPageSize());
        LambdaQueryWrapper<SysRole> queryWrapper = createQuery(param);
        resultPage = page(resultPage, queryWrapper);
        return BasePageListConvert.INSTANCE.convert(resultPage);
    }

    /**
     * 根据用户id 获取角色别名列表
     *
     * @param userId 用户ID
     * @return
     */
    @Override
    public List<String> listAliasByUser(Long userId) {
        return this.baseMapper.listAliasByUser(userId);
    }

    /**
     * 根据用户id 获取角色列表
     *
     * @param userId 用户ID
     * @return List<SysRole>
     */
    @Override
    public List<SysRole> listRolesByUserId(Long userId) {
        return this.baseMapper.listRolesByUserId(userId);
    }

    private LambdaQueryWrapper<SysRole> createQuery(SysRoleReqVO param) {
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper();
        if (StrUtil.isNotEmpty(param.getRoleName())) {
            queryWrapper.like(SysRole::getRoleName, param.getRoleName());
        }

        return queryWrapper;
    }
}
